package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.TaskPickupDispatchDTO;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.entity.TaskPickupDispatch;
import com.itheima.pinda.enums.pickuptask.PickupDispatchTaskAssignedStatus;
import com.itheima.pinda.enums.pickuptask.PickupDispatchTaskStatus;
import com.itheima.pinda.mapper.TaskPickupDispatchMapper;
import com.itheima.pinda.service.ITaskPickupDispatchService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 取件、派件任务信息表 服务实现类
 * </p>
 *
 * @author jpf
 * @since 2019-12-30
 */
@Service
public class TaskPickupDispatchServiceImpl extends ServiceImpl<TaskPickupDispatchMapper, TaskPickupDispatch> implements ITaskPickupDispatchService {
    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private TaskPickupDispatchMapper taskPickupDispatchMapper;

    @Override
    public TaskPickupDispatchDTO insert(TaskPickupDispatchDTO dto) {

        TaskPickupDispatch taskPickupDispatch = new TaskPickupDispatch();
        BeanUtils.copyProperties(dto, taskPickupDispatch);

        taskPickupDispatch.setId(idGenerator.nextId(taskPickupDispatch) + "");
        taskPickupDispatch.setCancelTime(LocalDateTime.now());
        // 任务状态
        taskPickupDispatch.setStatus(PickupDispatchTaskStatus.PENDING.getCode());
        // 任务分配状态
        taskPickupDispatch.setAssignedStatus(PickupDispatchTaskAssignedStatus.TO_BE_DISTRIBUTED.getCode());

        taskPickupDispatchMapper.insert(taskPickupDispatch);

        BeanUtils.copyProperties(taskPickupDispatch, dto);
        return dto;
    }

    @Override
    public TaskPickupDispatchDTO update(TaskPickupDispatchDTO dto) {
        TaskPickupDispatch taskPickupDispatch = new TaskPickupDispatch();
        BeanUtils.copyProperties(dto, taskPickupDispatch);
        taskPickupDispatchMapper.updateById(taskPickupDispatch);
        return dto;
    }

    @Override
    public IPage findByPage(Integer pageNum, Integer pageSize, TaskPickupDispatchDTO dto) {

        Page<TaskPickupDispatch> page = new Page<>(pageNum, pageSize);
        Page<TaskPickupDispatch> taskPickupDispatchPage = taskPickupDispatchMapper.selectPage(
                page, new LambdaQueryWrapper<TaskPickupDispatch>()
                        .eq(ObjectUtils.isNotEmpty(dto.getTaskType()), TaskPickupDispatch::getTaskType, dto.getTaskType())
                        .eq(ObjectUtils.isNotEmpty(dto.getStatus()), TaskPickupDispatch::getStatus, dto.getStatus())
                        .eq(ObjectUtils.isNotEmpty(dto.getSignStatus()), TaskPickupDispatch::getSignStatus, dto.getSignStatus())
                        .eq(ObjectUtils.isNotEmpty(dto.getAgencyId()), TaskPickupDispatch::getAgencyId, dto.getAgencyId())
                        .eq(ObjectUtils.isNotEmpty(dto.getCourierId()), TaskPickupDispatch::getCourierId, dto.getCourierId())
                        .eq(ObjectUtils.isNotEmpty(dto.getAssignedStatus()), TaskPickupDispatch::getAssignedStatus, dto.getAssignedStatus())
                        .orderByDesc(TaskPickupDispatch::getCreateTime));

        List<TaskPickupDispatch> records = taskPickupDispatchPage.getRecords();

        List<TaskPickupDispatchDTO> dtos = records.stream().map(taskPickupDispatch -> {
            TaskPickupDispatchDTO taskPickupDispatchDTO = new TaskPickupDispatchDTO();
            BeanUtils.copyProperties(taskPickupDispatch, taskPickupDispatchDTO);
            return taskPickupDispatchDTO;
        }).collect(Collectors.toList());

        return new Page<TaskPickupDispatchDTO>().setRecords(dtos);
    }

    @Override
    public List<TaskPickupDispatchDTO> findAll(TaskPickupDispatchDTO dto) {

        List<TaskPickupDispatch> list = taskPickupDispatchMapper.selectList(new LambdaQueryWrapper<TaskPickupDispatch>()
                .eq(ObjectUtils.isNotEmpty(dto.getTaskType()), TaskPickupDispatch::getTaskType, dto.getTaskType())
                .eq(ObjectUtils.isNotEmpty(dto.getStatus()), TaskPickupDispatch::getStatus, dto.getStatus())
                .eq(ObjectUtils.isNotEmpty(dto.getSignStatus()), TaskPickupDispatch::getSignStatus, dto.getSignStatus())
                .eq(ObjectUtils.isNotEmpty(dto.getAgencyId()), TaskPickupDispatch::getAgencyId, dto.getAgencyId())
                .eq(ObjectUtils.isNotEmpty(dto.getCourierId()), TaskPickupDispatch::getCourierId, dto.getCourierId())
                .eq(ObjectUtils.isNotEmpty(dto.getAssignedStatus()), TaskPickupDispatch::getAssignedStatus, dto.getAssignedStatus())
                .orderByDesc(TaskPickupDispatch::getCreateTime));

        List<TaskPickupDispatchDTO> collect = list.stream().map(taskPickupDispatch -> {
            TaskPickupDispatchDTO taskPickupDispatchDTO = new TaskPickupDispatchDTO();
            BeanUtils.copyProperties(taskPickupDispatch, taskPickupDispatchDTO);
            return taskPickupDispatchDTO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public TaskPickupDispatchDTO findDetail(String id, Integer taskType, String orderId) {
        TaskPickupDispatch taskPickupDispatch =
                taskPickupDispatchMapper.selectOne(
                        new LambdaQueryWrapper<TaskPickupDispatch>()
                                .eq(StringUtils.isNotEmpty(id),TaskPickupDispatch::getId,id)
                                .eq(ObjectUtils.isNotEmpty(taskType),TaskPickupDispatch::getTaskType,taskType)
                                .eq(StringUtils.isNotEmpty(orderId),TaskPickupDispatch::getOrderId,orderId));
        TaskPickupDispatchDTO taskPickupDispatchDTO = new TaskPickupDispatchDTO();
        BeanUtils.copyProperties(taskPickupDispatch,taskPickupDispatchDTO);
        return taskPickupDispatchDTO;
    }
}
